﻿using System;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMLib;
namespace BrainMatch
{
    public class WaveManager
    {
        private WaveDataSet m_WaveSet = new WaveDataSet();
        private SqlConnection m_Connection = null;
        private WaveDataSetTableAdapters.labelsTableAdapter dataAdapter = null;
        private WaveDataSetTableAdapters.label_namesTableAdapter namesAdapter = null;
        private WaveDataSetTableAdapters.label_qualityTableAdapter qualityAdapter = null;

        private IWaveClassify m_Matcher =null;// new HisClassify(this);
        public IWaveClassify Mathcer
        {
            get
            {
                return m_Matcher;
            }
        }

        public WaveDataSet Dataset
        {
            get
            {
                return m_WaveSet;
            }
        }

        private MainWindow m_MainWindow = null;
        public MainWindow Windows
        {
            get
            {
                return m_MainWindow;
            }
            set
            {
                m_MainWindow = value;
            }
        }
        public WaveManager(MainWindow window)
        {
            m_MainWindow = window;
            m_Matcher = new HisClassify(this);
            CreateDataObject();
        }

        public void InsertData(WaveItem item)
        {
            
            //处理重复数据
            string graphics_data = MakeDataString(item.FingerData);
            DataRow[] rows = m_WaveSet.labels.Select(string.Format("graphics_data='{0}'", graphics_data));
            if (rows.Length > 0)
            {
                WaveDataSet.labelsRow row = rows[0] as WaveDataSet.labelsRow;// m_WaveSet.labels.NewlabelsRow();
                row.name_id = item.TypeID;
                row.graphics_data = MakeDataString(item.FingerData);
                row.finger_data = m_Matcher.GetFingerData(item);
                row.quality_id = item.QualityID;
            }
            else
            {
                WaveDataSet.labelsRow row = m_WaveSet.labels.NewlabelsRow();
                row.name_id = item.TypeID;
                row.graphics_data = MakeDataString(item.FingerData);
                row.finger_data = m_Matcher.GetFingerData(item);
                row.quality_id = item.QualityID;
                m_WaveSet.labels.AddlabelsRow(row);
            }


            

            SaveData();
        }
        public void InsertName(string name, Color color,string remark)
        {
            WaveDataSet.label_namesRow row = m_WaveSet.label_names.Newlabel_namesRow();
            row.color = string.Format("{0},{1},{2}",color.R,color.G,color.B);
            row.name = name;
            m_WaveSet.label_names.Addlabel_namesRow(row);
            SaveData();
        }

        public void MatchWaves(WaveFileObject fileobject)
        {
            foreach (var item in fileobject.Waves)
            {
                MatchWave(item);
            }
        }

        public void MatchWave(WaveItem item)
        {
            List<MatchResult> result = m_Matcher.Match(item);
        }
        public void DeleteLabel(int id)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = m_Connection;
            cmd.CommandText = string.Format("delete from labels where id={0}",id);

            m_Connection.Open();
            cmd.ExecuteNonQuery();
            m_Connection.Close();

            dataAdapter.Fill(m_WaveSet.labels);
            namesAdapter.Fill(m_WaveSet.label_names);
        }
        public void LoadLabels()
        {
            dataAdapter.Fill(m_WaveSet.labels);
        }
        public void SaveData()
        {
            try
            {
               // m_Connection.Open();
                dataAdapter.Update(m_WaveSet.labels);
                namesAdapter.Update(m_WaveSet.label_names);
                qualityAdapter.Update(m_WaveSet.label_quality);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        private string MakeDataString(double[] data)
        {
            if (data.Length <= 0) return "";
            string datastr = data[0].ToString();
            for (int i =1; i < data.Length; i++)
            {
                datastr += ",";
                datastr += data[i].ToString();
            }
            return datastr;
        }
        private void CreateDataObject()
        {
            try
            {
                string connectionstring = string.Format(Properties.Settings.Default.DataConnection, Properties.Settings.Default.ServerName,
                                                                                                 Properties.Settings.Default.Database,
                                                                                                 Properties.Settings.Default.Username,
                                                                                                 Properties.Settings.Default.Password);
                //m_Connection = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=hltest;Integrated Security=True");
                m_Connection = new SqlConnection(connectionstring);
                dataAdapter = new WaveDataSetTableAdapters.labelsTableAdapter();
                dataAdapter.Connection = m_Connection;

                namesAdapter = new WaveDataSetTableAdapters.label_namesTableAdapter();
                namesAdapter.Connection = m_Connection;

                qualityAdapter = new WaveDataSetTableAdapters.label_qualityTableAdapter();
                qualityAdapter.Connection = m_Connection;

                dataAdapter.Fill(m_WaveSet.labels);
                namesAdapter.Fill(m_WaveSet.label_names);
                qualityAdapter.Fill(m_WaveSet.label_quality);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                DatabaseConfigDialog dataconfig = new DatabaseConfigDialog();
                if (dataconfig.ShowDialog() == DialogResult.OK)
                {
                    CreateDataObject();
                }
                else
                {
                    Application.Exit();
                }
            }
        }
    }
}
